<template>
    <div class="debris-list" v-loading="loading">
        <div class="input-box" style="position: relative">
            <el-input
                placeholder="搜索趣ID、QQ号、游戏昵称"
                @keyup.enter.native="selectChange"
                style="width:240px;"
                clearable
                v-model.trim="paramsList.keyWord">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <el-button type="primary" class="btn" style="margin-left:20px" @click="getList(1)">查询</el-button>
            <span class="input-title">段位</span>
            <el-select @change="selectChange" style="width:150px;" v-model="paramsList.jobLevel">
                <el-option label="全部" :value="null"></el-option>
                <el-option v-for="(item, index) in gradeList" :key="index" :label="item.label" :value="item.value"></el-option>
            </el-select>
            <span class="input-title">最高价值</span>
            <el-select @change="selectChange" style="width:150px;" v-model="paramsList.gem">
                <el-option label="全部" :value="null"></el-option>
                <el-option v-for="(item, index) in worthList" :key="index" :label="item.label" :value="item.value"></el-option>
            </el-select>
            <span class="input-title">账号状态</span>
            <el-select @change="selectChange" style="width:150px;" v-model="paramsList.manageStatus">
                <el-option label="全部" :value="null"></el-option>
                <el-option v-for="(item, index) in manageStatusList" :key="index" :label="item.label" :value="item.value"></el-option>
            </el-select>
            <!-- 添加平台 -->
            <span class="input-title">平台</span>
            <el-select v-model="areaType" placeholder="请选择" @change="typeStatusChange">
            <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
            </el-select>
            <!-- <el-button type="success" class="btn" @click="exportData" style="position: absolute; right: 10px">导出</el-button> -->
        </div>
        <div class="table-box">
            <el-table :data="tableData" ref="table" @sort-change="sortChange" border style="width: 100%" :span-method="tableMerge">
                <el-table-column fixed width="100" prop="userId" label="趣ID"></el-table-column>
                <el-table-column width="150" prop="accountNo" label="QQ/昵称/平台">
                    <template slot-scope="scope">
                        <div>QQ：{{ scope.row.accountNo }}</div>
                        <div>昵称：{{ scope.row.roleName }}</div>
                        <div>平台：{{ scope.row.areaType === 1 ? '安卓' : scope.row.areaType === 2 ? 'IOS' : '-' }}</div>
                        <div v-if="scope.row.accountBlackDesc" style="color: red"> {{ scope.row.accountBlackDesc }}</div>
                    </template>
                </el-table-column>
                <!-- <el-table-column min-width="100" prop="roleName" label="游戏昵称"></el-table-column> -->
                <el-table-column min-width="140" prop="accountValue" sortable label="账号估值（元）"></el-table-column>
                <el-table-column min-width="120" label="英雄数/皮肤数">
                    <template slot-scope="scope">
                        <span>{{ scope.row.heroSize  || '-' }} / {{ scope.row.skinSize  || '-' }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="140" prop="totalIncome" sortable label="累计收益（元）">
                    <template slot-scope="scope">
                        <span v-if="scope.row.totalIncome">{{ scope.row.totalIncome / 10000 }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="140" label="托管天数" prop="managerDay"  sortable="custom">
                    <template slot-scope="scope">
                        <span v-if="scope.row.managerDay">{{ scope.row.managerDay }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="160" prop="totalDate" sortable label="总出租时长（时）">
                    <template slot-scope="scope">
                        <span v-if="scope.row.totalDate">{{ formatTime(scope.row.totalDate) }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="110" prop="totalCount" sortable label="总出租次数">
                    <template slot-scope="scope">
                        <span>{{ scope.row.totalCount || '-' }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" prop="currentPrice" sortable label="当前价格">
                    <template slot-scope="scope">
                        <span v-if="scope.row.currentPrice">{{ scope.row.currentPrice | numInitW }}趣宝石</span>
                        <span v-else>-</span>
                        <i class="el-icon-edit" style="color:#409EFF; cursor:pointer;" @click="updateAccountPrice(true, scope)"></i>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" prop="startPrice" sortable label="起始价格">
                    <template slot-scope="scope">
                        <span v-if="scope.row.startPrice">{{ scope.row.startPrice | numInitW }}趣宝石</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="段位">
                    <template slot-scope="scope">
                        <span>{{ scope.row.roleJob }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="信誉分">
                    <template slot-scope="scope">
                        <span>{{ scope.row.credit }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="托管时间" min-width="120">
                    <template slot-scope="scope">
                        <span v-if="scope.row.createTime">{{ formatDate(scope.row.createTime)  }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <!--  -->
                <el-table-column min-width="100" prop="status" label="状态">
                    <template slot-scope="scope">
                        <div v-if="scope.row.exceptionStatus === 1">
                            <span style="color: #F56C6C;">账号异常</span>
                        </div>
                        <div v-else>
                            <span v-if="scope.row.manageStatus === 0">待托管</span>
                            <span v-else-if="scope.row.manageStatus === -1">暂停托管</span>
                            <span v-else-if="scope.row.manageStatus === -2" style="color: #F56C6C;">注销托管</span>
                            <span v-else-if="scope.row.manageStatus === 1" style="color:#67C23A;">已托管</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <div v-if="scope.row.manageStatus === 0 || scope.row.manageStatus === -1">
                            <el-button style="margin-left: 10px;color:red;" @click="openDialog(scope.row, -2)" type="text" size="small">注销托管</el-button>
                            <el-button @click="changeStatus(scope.row, 1)" type="text" size="small" style="color:#67C23A;">开始托管</el-button>
                        </div>
                        <div v-if="scope.row.manageStatus === 1">
                            <el-button style="margin-left: 10px;color:red;" @click="openDialog(scope.row, -2)" type="text" size="small">注销托管</el-button>
                            <el-button @click="openDialog(scope.row, -1)" type="text" size="small" style="color:#909399;">暂停托管</el-button>
                        </div>
                        <div v-if="scope.row.manageStatus === -2">
                            <el-button style="margin-left: 10px;color:#67C23A;" @click="changeStatus(scope.row, 1)" type="text" size="small">恢复托管</el-button>
                        </div>
                        <div>
                            <el-button style="margin-left: 10px;" @click="goDetail(scope.row)" type="text" size="small">查看详情</el-button>
                        </div>
                        <div v-if="scope.row.exceptionStatus === 0">
                            <el-button style="margin-left: 10px;" @click="abnormal(scope)" type="text" size="small">账号异常</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog
            class="dialog-box"
            title="暂停/注销理由:"
            :visible.sync="dialogVisible"
            width="500px">
            <div>
                <el-input type="textarea" maxlength="20" rows="3" v-model.trim="reason"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogCancel">取 消</el-button>
                <el-button type="primary" @click="changeStatusPauseOff(digItem, digStatus)">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
            class="dialog-box"
            title="账号异常:"
            :visible.sync="abnormalVisible"
            width="500px">
            <div>
                <el-input type="textarea" maxlength="20" rows="3" v-model.trim="abnormalData.message"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="abnormalVisible = false">取 消</el-button>
                <el-button type="primary" @click="setAbnormal">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 修改价格弹框 -->
        <el-dialog
            class="dialog-box"
            title="请输入价格"
            :visible.sync="priceVisible"
            width="500px">
            <div v-if="priceInfo" class="price-list">
                <span class="price">当前价格: </span>
                <span>{{ priceInfo.currentPrice}}趣宝石</span>
            </div>
            <div class="price-list">
                <span class="price">价格修改为：</span>
                <el-input class="n-input" type="number" v-model.trim="updatePriceNumber"></el-input>
                <span class="price2">趣宝石</span>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateAccountPrice(false)">取 消</el-button>
                <el-button type="primary" @click="conFirmPrice()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
import { export2Excel } from '@/excel/util'

export default {
    data () {
        return {
            abnormalVisible: false,
            paramsList: {
                jobLevel: null,
                keyWord: null,
                gem: null,
                manageStatus: null
            },
            digStatus: null,
            digItem: {},
            gradeList: [
                {
                    value: 100,
                    label: '王者'
                },
                {
                    value: 101,
                    label: '星耀'
                },
                {
                    value: 102,
                    label: '钻石'
                },
                {
                    value: 103,
                    label: '铂金'
                },
                {
                    value: 104,
                    label: '黄金'
                },
                {
                    value: 105,
                    label: '白银'
                },
                {
                    value: 106,
                    label: '青铜'
                }
            ],
            worthList: [
                {
                    value: 28,
                    label: '28'
                },
                {
                    value: 38,
                    label: '38'
                },
                {
                    value: 48,
                    label: '48'
                },
                {
                    value: 58,
                    label: '58'
                },
                {
                    value: 68,
                    label: '68'
                },
                {
                    value: 78,
                    label: '78'
                },
                {
                    value: 88,
                    label: '88'
                },
                {
                    value: 118,
                    label: '118'
                },
                {
                    value: 188,
                    label: '188'
                },
                {
                    value: 208,
                    label: '208'
                },
                {
                    value: 238,
                    label: '赛季专属'
                },
                {
                    value: 268,
                    label: '活动限定'
                },
                {
                    value: 298,
                    label: '荣耀典藏'
                }
            ],
            manageStatusList: [
                {
                    value: 0,
                    label: '待托管'
                },
                {
                    value: 1,
                    label: '已托管'
                },
                {
                    value: -1,
                    label: '暂停托管'
                },
                {
                    value: -2,
                    label: '注销托管'
                }
            ],
            areaType: 0,
            typeOptions: [ //平台选择字段
                {
                value: 0,
                label: '全部'
                },
                {
                value: 1,
                label: '安卓'
                },
                {
                value: 2,
                label: 'IOS'
                }
            ],
            tableData: [],
            reason: null,
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            dialogVisible: false,
            type: '',
            sort: '',
            abnormalData: {
                message: '',
                gameId: 1104466820
            },
            index: 0,
            priceVisible: false, // 修改价格flag
            priceInfo: {},
            updatePriceNumber: null
        }
    },
    activated () {
        this.getList(this.pageNum)
    },
    methods: {
        numInitMethod(val) {
            if (val >= 10000) {
                return (Math.floor(val / 1000) / 10) + 'w 趣宝石'
            } else {
                return val + ' 趣宝石'
            }
        },
        // 打印
        exportData() {
            this.columns = [{
                title: '趣ID', key: 'userId'
            },
            {
                title: 'QQ', key: 'accountNo'
            },
            // {
            //     title: '昵称', key: 'roleNameDesc'
            // },
            {
                title: '平台', key: 'areaTypeDesc'
            },
            {
                title: '账号估值（元）', key: 'accountValue'
            },
            {
                title: '英雄数', key: 'heroSize'
            },
            {
                title: '皮肤数', key: 'skinSize'
            },
            {
                title: '累计收益（元）', key: 'totalIncomeDesc'
            },
            {
                title: '托管天数', key: 'managerDay'
            },
            {
                title: '总出租时长（时）', key: 'totalDateDesc'
            },
            {
                title: '总出租次数', key: 'totalCount'
            },
            {
                title: '当前价格', key: 'currentPriceDesc'
            },
            {
                title: '起始价格', key: 'startPriceDesc'
            },
            {
                title: '段位', key: 'roleJob'
            },
            {
                title: '信誉分', key: 'credit'
            },
            {
                title: '托管时间', key: 'createTimeDesc'
            },
            {
                title: '状态', key: 'statusDesc'
            }
            ]
            this.getList(1, true)
        },
        // 修改价格
        updateAccountPrice (flag, scope) {
            this.priceVisible = flag
            this.priceInfo = (scope && scope.row) || {}
            if (!flag) {
                this.updatePriceNumber = null
            }
        },
        conFirmPrice () {
            if (!this.priceInfo) return
            let params = {
                dataId: this.priceInfo.dataId,
                perHourPrice: this.updatePriceNumber
            }
            this.$http.post('/rentPrice/updateAccountPrice', params).then(res => {
                if (res.code === '0') {
                    this.$message.success('更改价格成功')
                    this.updatePriceNumber = null
                    this.getList(1)
                } else {
                    this.$message({
                        type: 'error',
                        message: res.desc
                    })
                }
                this.priceVisible = false
            })
        },
        formatDate (time) {
            return formatDate(time, 'YYYY-MM-DD  HH:MM')
        },
        typeStatusChange (areaType) {
            this.areaType = areaType
            this.getList(1)
        },
        // 获取列表
        async getList (pageNum, flag) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum: pageNum,
                pageSize: this.pageSize,
                areaType: this.areaType
            }
            if (flag) {
                params.pageSize = 1000000
            }
            if (this.type) params.type = this.type
            if (this.sort) params.sort = this.sort
            params = Object.assign(params, this.paramsList)
            let res = await this.$http.get('/trade/account', {
                params
            })
            if (res.code === '0') {
                if (flag) {
                    res.data && res.data.forEach((item) => {
                        item.areaTypeDesc = item.areaType === 1 ? '安卓' : item.areaType === 2 ? 'IOS' : '-'
                        item.totalIncomeDesc = item.totalIncome / 10000
                        item.roleNameDesc = item.roleName.replace(/￿/,'')
                        item.totalDateDesc = this.formatTime(item.totalDate)
                        item.currentPriceDesc = this.numInitMethod(item.currentPrice)
                        item.startPriceDesc = this.numInitMethod(item.startPrice)
                        item.createTimeDesc = this.formatDate(item.createTime)
                        if (item.exceptionStatus === 1) {
                            item.statusDesc = '账号异常'
                        } else {
                            if (item.manageStatus === 0) {
                                item.statusDesc = '待托管'
                            }
                            if (item.manageStatus === -1) {
                                item.statusDesc = '暂停托管'
                            }
                            if (item.manageStatus === -2) {
                                item.statusDesc = '注销托管'
                            }
                            if (item.manageStatus === 1) {
                                item.statusDesc = '已托管'
                            }
                        }
                        
                    })
                    export2Excel(this.columns, res.data, `王者荣耀-租号账号列表`)
                } else {
                    this.tableData = res.data
                    this.total = res.count
                }
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        formatTime (time) {
            if (!time) return '0分'
            let m = 0
            let h = 0
            if (time < 60) {
                return `${time}分`
            } else {
                m = (time % 60)
                h = (time / 60).toFixed(0)
                return m > 0 ? `${h}小时${m}分` : `${h}小时`
            }
        },
        tableMerge ({ row, column, rowIndex, columnIndex }) {
            if (row.merge >= 0 && (columnIndex >= 9 || columnIndex <= 1)) {
                if (row.merge === 0) {
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                } else {
                    return {
                        rowspan: row.merge,
                        colspan: 1
                    }
                }
            }
        },
        // 后端排序
        sortChange ({ column, prop, order }) {
            if (order === 'descending') order = 'desc'
            if (order === 'ascending') order = 'asc'
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = ''
            } else {
                switch (column.label) {
                    case '皮肤数':
                        this.type = 'skinList'
                        break
                    default:
                        this.type = prop
                }
            }
            console.log(this.type, column, order)
            this.getList(1)
        },
        selectChange () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },
        changeStatus (item, status) {
            this.$http.post('/trade/updateAccountStatus', {
                accountNo: item.accountNo,
                status,
                remark: this.reason,
                userId: item.userId
            }).then(res => {
                if (res.code === '0') {
                    item.manageStatus = status
                    this.$message({
                        type: 'success',
                        message: '状态修改成功'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: res.desc
                    })
                }
            }).catch((err) => {
                this.$message({
                    type: 'error',
                    message: err.desc
                })
            })
        },
        openDialog (item, status) {
            this.dialogVisible = true
            this.digItem = item
            this.digStatus = status
        },
        changeStatusPauseOff () {
            if (!this.reason) {
                return this.$message({
                    type: 'warning',
                    message: '请填写暂停/注销理由'
                })
            }
            this.dialogVisible = false
            this.changeStatus(this.digItem, this.digStatus)
            this.reason = null
            this.digItem.manageStatus = this.digStatus
        },
        dialogCancel () {
            this.dialogVisible = false
            this.reason = null
        },
        goDetail (item) {
            let query = {
                accountId: item.accountNo,
                userId: item.userId
            }
            const { href } = this.$router.resolve({
                path: '/wzry/accDetail',
                query
            })
            window.open(href, '_blank')
        },
        abnormal (scope) {
            let data = {
                userId: scope.row.userId,
                accountNo: scope.row.accountNo
            }
            this.index = scope.$index
            Object.assign(this.abnormalData, data)
            this.abnormalVisible = true
        },
        setAbnormal () {
            this.$http.post('/trade/setAccountException', {
                ...this.abnormalData
            }).then(res => {
                if (res.code === '0') {
                    this.$message.success('已标记为异常')
                    this.$set(this.tableData[this.index], 'exceptionStatus', 1)
                } else {
                    this.$message({
                        type: 'error',
                        message: res.desc
                    })
                }
                this.abnormalVisible = false
            })
        }
    }
}
</script>

<style lang="scss" scoped>
    .price-list {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        margin-bottom: 20px;
        .price {
          display: inline-block;
          min-width: 100px;
        }
        .price2 {
          display: inline-block;
          min-width: 80px;
          margin-left: 20px;
        }
        /* 谷歌 */
        /deep/ .el-input__inner::-webkit-outer-spin-button,
        /deep/ .el-input__inner::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }
    }
    .debris-list {
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .desc {
                overflow: hidden;
                line-height: 20px;
                -webkit-line-clamp: 5;
                -webkit-box-orient: vertical;
                display: -webkit-box;
                height: 100px;
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
    .dialog-box {
        /deep/ .el-dialog__body {
            padding: 0 20px;
        }
    }
</style>
